//
//  JJHomeRetweetView.swift
//  JJSina
//
//  Created by mac on 2017/12/7.
//  Copyright © 2017年 xinyuan. All rights reserved.
//

import UIKit
import SnapKit

class JJHomeRetweetView: UIView {

    var retweetViewBottomConstraint: Constraint?
    
    var statusModel: JJStatusViewModel? {
        didSet{
            contentLabel.text = statusModel?.homeModel?.retweeted_status?.text
            
            retweetViewBottomConstraint?.deactivate()
            
            if let picUrls = statusModel?.homeModel?.retweeted_status?.pic_urls, picUrls.count > 0 {
                // 赋值
                pictureView.pic_urls = picUrls
                // 设置约束
                self.snp.makeConstraints { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                }
                // 显示配图
                pictureView.isHidden = false
            } else {
                // 设置约束
                self.snp.makeConstraints { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
                // 隐藏配图
                pictureView.isHidden = true
            }
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    func setupUI() {
        backgroundColor = JJRandomColor()
        addSubview(contentLabel)
        addSubview(pictureView)
        contentLabel.snp.makeConstraints { (make) in
            make.left.top.equalTo(10)
            make.width.equalTo(JJ_SCREEN_W - 20)
        }
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(10)
        make.top.equalTo(contentLabel.snp.bottom).offset(10)
            // 在配图内容已经计算好了
            //            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        // 约束转发微博的底部
        snp.makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
     // MARK: - 懒加载子控件
    private lazy var contentLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.black
        lab.font = UIFont.systemFont(ofSize: JJ_MIDDLE_FONTSIZE)
        lab.numberOfLines = 0
        lab.backgroundColor = UIColor.white
        return lab
    }()
    // 配图
    private lazy var pictureView: JJHomePictureView = {
        let view = JJHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}













